package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.Memberinfo;
import com.common.Common;
import com.dao.MemberDao;
import com.utils.PageInfo;
import com.utils.PageUtil;
import com.utils.StringUtil;

/**
 * 会员维护
 * @author 14560
 *
 */
@WebServlet("/MemberServlet.do")
public class MemberServlet extends HttpServlet {
	private MemberDao _menberDao=new MemberDao();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		String flag = request.getParameter("flag");
		if ("manage".equals(flag)) {
			manage(request, response);
		}else if ("delete".equals(flag)) {
			delete(request, response);
		}else if ("deleteAll".equals(flag)) {
			deleteAll(request, response);
		}if ("show".equals(flag)) {
			show(request, response);
		}
	}
	
	/*
	 * 会员管理
	 */
	private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String memberName = request.getParameter("memberName");
		if(!StringUtil.isEmpty(memberName)) {
			memberName = memberName.trim();
		}
		String beginDate = request.getParameter("beginDate");
		if(!StringUtil.isEmpty(beginDate)) {
			beginDate = beginDate.trim();
		}
		String endDate = request.getParameter("endDate");
		if(!StringUtil.isEmpty(endDate)) {
			endDate = endDate.trim();
		}
		int pageSize = Common.PAGE_SIZE;
		int rowCount = _menberDao.getMemberCount(memberName, beginDate, endDate);
		int pageIndex = 1;
		String pageIndexStr = request.getParameter("pageIndex");
		if (pageIndexStr != null) {
			pageIndex = Integer.parseInt(pageIndexStr);
		}
		PageInfo page = PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
		List<Memberinfo> memberList = _menberDao.getMemberList(memberName, beginDate, endDate,page);
		request.setAttribute("page", page);
		request.setAttribute("memberList", memberList);
		request.getRequestDispatcher("/member/member_manage.jsp").forward(request, response);

	}
	
	/*
	 * 删除会员
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		int rest = _menberDao.delMember(idnumber);
		if (rest == 1) {
			manage(request, response);
		}
	}

	/*
	 * 批量删除会员
	 */
	private void deleteAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String[] idList = request.getParameterValues("ck_id");
		_menberDao.delAllMember(idList);
		manage(request, response);

	}
	
	/*
	 * 查看会员详情
	 */
	private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		Memberinfo member=_menberDao.getMember(idnumber);
		String ipString=member.getIp();
		if(ipString!=null) {
			member.setIpLists(ipString.split(","));
		}
		request.setAttribute("member", member);
		request.getRequestDispatcher("/member/member_show.jsp").forward(request, response);
	}
	
	
}
